'use strict';
var app = angular.module('app', ['ui.router', 'oc.lazyLoad']);

app.config(["$stateProvider", "$urlRouterProvider", function ($stateProvider, $urlRouterProvider) {
    //路由配置
    $urlRouterProvider.otherwise('/login');
    $stateProvider.state("main", {
        abstract: true,
        url: '',
        templateUrl: 'src/app/admin/index_admin.html',
        controller: 'adminController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/adminController.js'
                ]);
            }]
        }
    }).state("login", {
        url: '/login',
        templateUrl: 'src/app/admin/login/login.html',
        controller: 'loginController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/login/login.js'
                ]);
            }]
        }
    }).state("register", {
        url: '/register',
        templateUrl: 'src/app/admin/login/register.html',
        controller: 'registerController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/login/register.js'
                ]);
            }]
        }
    }).state("forgotton", {
        url: '/forgotton',
        templateUrl: 'src/app/admin/login/forgotton-password.html',
        controller: 'forgottonController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/login/forgotton-password.js'
                ]);
            }]
        }
    }).state("main.monitoring", {
        url: '/monitoring',
        templateUrl: 'src/app/admin/monitoring/monitoring.html',
        controller: 'monitoringController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/monitoring/monitoring.js'
                ]);
            }]
        }
    }).state("main.write", {
        url: '/write',
        templateUrl: 'src/app/admin/article/article_write.html',
        controller: 'articleWriteController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/article/article_write.js'
                ]);
            }]
        }
    }).state("main.articleManage", {
        url: '/articleManage',
        templateUrl: 'src/app/admin/article/article_manage.html',
        controller: 'articleManageController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/article/article_manage.js'
                ]);
            }]
        }
    }).state("main.diary", {
        url: '/diary',
        templateUrl: 'src/app/admin/article/diary.html',
        controller: 'diaryController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/article/diary.js'
                ]);
            }]
        }
    }).state("main.drafts", {
        url: '/drafts',
        templateUrl: 'src/app/admin/article/drafts.html',
        controller: 'draftsController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/article/drafts.js'
                ]);
            }]
        }
    }).state("main.dustbin", {
        url: '/dustbin',
        templateUrl: 'src/app/admin/article/dustbin.html',
        controller: 'dustbinController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/article/dustbin.js'
                ]);
            }]
        }
    }).state("main.discuss", {
        url: '/discuss',
        templateUrl: 'src/app/admin/discuss/discuss.html',
        controller: 'discussController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/discuss/discuss.js'
                ]);
            }]
        }
    }).state("main.help", {
        url: '/help',
        templateUrl: 'src/app/admin/other/help.html',
        controller: 'helpController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/other/help.js'
                ]);
            }]
        }
    }).state("main.buildingTpl", {
        url: '/buildingTpl',
        templateUrl: 'src/app/admin/tool/building_tpl.html',
        controller: 'building_tplController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/tool/building_tpl.js'
                ]);
            }]
        }
    }).state("main.normal", {
        url: '/normal',
        templateUrl: 'src/app/admin/tool/normal.html',
        controller: 'normalController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/tool/normal.js'
                ]);
            }]
        }
    }).state("main.other", {
        url: '/other',
        templateUrl: 'src/app/admin/tool/other.html',
        controller: 'otherController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/tool/other.js'
                ]);
            }]
        }
    }).state("main.partition", {
        url: '/partition',
        templateUrl: 'src/app/admin/tool/partition.html',
        controller: 'partitionController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/tool/partition.js'
                ]);
            }]
        }
    }).state("main.template", {
        url: '/template',
        templateUrl: 'src/app/admin/tool/template.html',
        controller: 'templateController',
        resolve: {
            load: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([
                    'src/app/admin/tool/template.js'
                ]);
            }]
        }
    });
}
]).controller("navCtrl",function($rootScope,$state) {
    $.ajax({
        url : '/user/read/promission',
        success : function(result) {
            if (result.httpCode == 200) {
                $rootScope.userInfo = result.user;
                $rootScope.menuList = result.menus;
                $rootScope.$apply();
                console.info($rootScope.userInfo);
            }
        }
    });
}).run(['$rootScope', '$state', '$stateParams', '$timeout', '$templateCache',
    function ($rootScope, $state, $stateParams, $timeout, $templateCache) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
            var from = fromState.name, to = toState.name;
            if (from && to) { // 解决 相应模块从列表进入编辑后 状态丢失问题
                var s1 = from.substring(0, from.lastIndexOf(".")),
                    g1 = from.substring(from.lastIndexOf(".") + 1),
                    s2 = to.substring(0, to.lastIndexOf(".")),
                    g2 = to.substring(to.lastIndexOf(".") + 1);
                if (s1 == s2) {
                    if (g1 == 'list' && (g2 == 'update' || g2 == 'view')) { //进行编辑
                        toParams['params'] = window.location.hash;
                    }
                    //返回列表
                    if ((g1 == "update" || g1 == 'view') && g2 == 'list') {
                        var h = fromParams['params'];
                        if (h) {
                            $timeout(function () {
                                window.location.hash = h;
                            });
                        }
                    }
                }
            }
        });
    }]);
